package com.socialuni.sdk.factory.user.base;

import com.socialuni.sdk.api.model.RO.user.base.SocialUserFollowDetailRO;
import com.socialuni.sdk.dao.FollowDao;
import com.socialuni.sdk.model.DO.user.UserDO;
import com.socialuni.sdk.model.DO.user.UserFollowDetailDO;
import com.socialuni.sdk.repository.UserFollowDetailRepository;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class SocialFollowUserROFactory {
    static FollowDao followDao;
    static UserFollowDetailRepository userFollowDetailRepository;

    @Resource
    public void setFollowDao(FollowDao followDao) {
        SocialFollowUserROFactory.followDao = followDao;
    }

    @Resource
    public void setUserFollowDetailRepository(UserFollowDetailRepository userFollowDetailRepository) {
        SocialFollowUserROFactory.userFollowDetailRepository = userFollowDetailRepository;
    }

    public static SocialUserFollowDetailRO newSocialFollowUserRO(UserDO user, UserDO mineUser) {
        SocialUserFollowDetailRO userRO = new SocialUserFollowDetailRO(SocialContentUserROFactory.newContentUserRO(user, mineUser));

        userRO.setHasFollowed(false);
        userRO.setHasBeFollowed(false);

        if (mineUser != null && !userRO.getIsMine()) {
            boolean hasFollowUser = followDao.userHasFollowBeUser(mineUser.getId(), user.getId());
            userRO.setHasFollowed(hasFollowUser);

            boolean hasBeFollowed = followDao.userHasFollowBeUser(user.getId(), mineUser.getId());
            userRO.setHasBeFollowed(hasBeFollowed);
        }
        UserFollowDetailDO userFollowDetailDO = userFollowDetailRepository.findByUserId(user.getId());
        //用户关注粉丝数
        userRO.setFansNum(userFollowDetailDO.getFansNum());
        if (mineUser != null && userRO.getIsMine()) {
            userRO.setFollowNum(userFollowDetailDO.getFollowNum());
        }
        return userRO;
    }

}
